//
//  ViewController.m
//  CarPlateInputViewDemo
//
//  Created by ilongge on 2020/10/22.
//

#import "ViewController.h"
#import "GDCarPlateInputView.h"
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import "SendMessageView.h"

@interface ViewController ()
<
UITextFieldDelegate,
UITextViewDelegate
>
@property (weak, nonatomic) IBOutlet UITextField *carPlateTextField;
@property (weak, nonatomic) IBOutlet UITextView *messageView;
@property (nonatomic, strong) SendMessageView *sendMessageView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    if (self.sendMessageView == nil) {
        self.sendMessageView = [SendMessageView sendMessageView];
    }
    self.sendMessageView.orginFrame = CGRectMake(0, self.view.frame.size.height - 40, self.view.frame.size.width, 40);
    [self.view addSubview:self.sendMessageView];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view.window endEditing:YES];
}

- (NSData *)convertHexStrToData:(NSString *)str
{
    if (!str || [str length] == 0) {
        return nil;
    }
    NSMutableData *hexData = [[NSMutableData alloc] initWithCapacity:20];
    NSRange range;
    if ([str length] % 2 == 0) {
        range = NSMakeRange(0, 2);
    } else {
        range = NSMakeRange(0, 1);
    }
    for (NSInteger i = range.location; i < [str length]; i += 2) {
        unsigned int anInt;
        NSString *hexCharStr = [str substringWithRange:range];
        NSScanner *scanner = [[NSScanner alloc] initWithString:hexCharStr];
        
        [scanner scanHexInt:&anInt];
        NSData *entity = [[NSData alloc] initWithBytes:&anInt length:1];
        [hexData appendData:entity];
        
        range.location += range.length;
        range.length = 2;
    }
    return hexData;
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    if (!self.carPlateTextField.isFirstResponder) {
        [self.carPlateTextField becomeFirstResponder];
    }
}
#pragma mark UITextFieldDelegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField == self.carPlateTextField ) {
        CGFloat inputHeight = kBottomSafeHeight + 300;
        CGRect inputRect = CGRectMake(0, self.view.frame.size.height - inputHeight, SCREEN_WIDTH, inputHeight);
        GDCarPlateInputView *inputView = [[GDCarPlateInputView alloc] initWithFrame:inputRect];
        inputView.carPlate = self.carPlateTextField.text;
        textField.inputView = inputView;
        [textField reloadInputViews];
        WS(weakSelf);
        inputView.doneButtonBlock = ^(NSString *stringValue) {
            StrongSelf(strongSelf);
            [strongSelf carPlateInputDone:stringValue];
        };
        
        inputView.cancelButtonBlock = ^{
            StrongSelf(strongSelf);
            [strongSelf carPlateInputCancel];
        };
        
        inputView.specialButtonBlock = ^{
            StrongSelf(strongSelf);
            [strongSelf carPlateInputSpecial];
        };
    }
}

-(void)textViewDidBeginEditing:(UITextView *)textView{
    self.sendMessageView.isEditing = YES;
    self.sendMessageView.message = textView.text;
}

- (void)carPlateInputDone:(NSString *)stringValue
{
    [self.view.window endEditing:YES];
    self.carPlateTextField.inputView = nil;
}

- (void)carPlateInputCancel
{
    [self.view.window endEditing:YES];
}

- (void)carPlateInputSpecial
{
    [self.view.window endEditing:YES];
    self.carPlateTextField.inputView = nil;
    [self.carPlateTextField becomeFirstResponder];
    [self.carPlateTextField reloadInputViews];
}

@end
